package com.example.bloodbankapplication.fragment;

import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.example.bloodbankapplication.DashBoardActivity;
import com.example.bloodbankapplication.R;
import com.example.bloodbankapplication.service.BloodBankService;

public class LoginFragment   extends BaseFragment{
	
	private MyReceiver mReceiver;
	private EditText etUserId,etPwd;
	private Button btLogin;
	private final String ACTION ="LoginFragment";
	
	private Editor mEditor;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.login_fragment, null);    
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mReceiver = new MyReceiver();
		etUserId  = (EditText) getView().findViewById(R.id.userid);
		etPwd  = (EditText) getView().findViewById(R.id.pwd);
		etPwd.setOnEditorActionListener(new DoneOnEditorActionListener());
		btLogin  = (Button) getView().findViewById(R.id.login);
		btLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				getLogin();
			}
		});
	}
	
	class DoneOnEditorActionListener implements OnEditorActionListener {
	    @Override
	    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
	        if (actionId == EditorInfo.IME_ACTION_DONE) {
	        	hideSoftKeyPad();
	        	getLogin();
	        	
	            return true;    
	        }
	        return false;
	    }
	}
	
	private void getLogin(){
		

		
		String user = etUserId.getText().toString();
		String password = etPwd.getText().toString();
		String loginUrl = "https://login.salesforce.com/services/oauth2/token?grant_type=password&" +
		"client_id=3MVG9Y6d_Btp4xp6aEkEMnRIQkoIjfP3MEejXf0gJWU7PONoICDPZcxWTkLzNGi8xo5eFVJd_ZrehVPgfjIST&" +
		"client_secret=3951004059433787576&username="+user+"&password="+password+"1uqg6tWQzt4rAYmpzAgRwV1tD";
		
		Log.i("NCS","Login Url is :"+loginUrl);
		
		Intent intent = new Intent(getActivity(),BloodBankService.class);
		intent.putExtra("URL", loginUrl);
		intent.putExtra("Action", ACTION);
		intent.putExtra("Screen", "LoginFragment");
		intent.putExtra("Type", "");
		
		startProgressDialog();
		getActivity().startService(intent);
		
		
		
	
	}
	
	@Override
	public void onResume() {
		getActivity().registerReceiver(mReceiver, new IntentFilter(ACTION));
		super.onResume();
	}
	
	@Override
	public void onDestroy() {
		getActivity().unregisterReceiver(mReceiver);
		super.onDestroy();
	}
	
	class MyReceiver  extends BroadcastReceiver{

		@Override
		public void onReceive(Context arg0, Intent intent) {
			
			stopProgressDialog();
			
			if(intent.getStringExtra("Key")!=null){
			
			try{
			JSONObject jObjct = new JSONObject(intent.getStringExtra("Key"));
			String accesstoken = jObjct.getString("access_token");
			String instanceUrl = jObjct.getString("instance_url");
			
			
			mEditor = mPreferance.edit();
			mEditor.putString("Access", accesstoken);
			mEditor.putString("Instance", instanceUrl);
			mEditor.commit();
			
			}catch(Exception ex){
				
			}
			
			startActivity(new Intent(getActivity(),DashBoardActivity.class));}else{
				
				showPopUp(getActivity(), getResources().getString(R.string.invalid));
				etUserId.setText("");
				etPwd.setText("");
			}
			
		}
		
	}
	

}
